/*----------------------------------------------
| noka select v1.0 www.97521.com               |
| rebin 2014-08-11                             |
|---------------------------------------------*/
var ndateutil = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.cfg = cfg;					//配置信息
    	this.id=cfg.id; //id
    	this.allownull = cfg.allownull;//为true表示可以为空
    	this.hImg;
    	this.mImg;
    	this.prave = cfg.prave;
    	this.disabled = cfg.disabled;
    	this.readonly = cfg.readonly;
    },
    show : function(){
    	var idx = 0;
        var html = [];
        var self = this;
        var bodyHtml = $(self.id+'_dadiv');
        this.PreLoding();
        if(!self.allownull){
        	html[idx++] = '<div style="float:left;color: red;" class="nk_input_lineheight">*</div>';
        	html[idx++] = '<img style="display:none;vertical-align:middle" class="nk_input_imgheight" id="'+self.id+'_img" src="'+self.cfg.rurl+'/nokatag/formcheck/images/exclamation.gif"></img>';
        }
        bodyHtml.innerHTML=html.join('');
        self.extEvent();
    },
    resetWidth : function(){
    	var self = this;
    	var width = $(self.id).getWidth()-0;
    	$(self.id).setStyle({width:width+'px'});
    },
    extEvent : function(){
    	var self = this;
    	$(self.id+'_sdDiv').observe('click',function(){//失失焦点时验证其值
    		if('readonly'==self.readonly || 'disabled'==self.disabled){
    			return;
    		}
    		try{
	    		self.prave.onHideDate =function(){
	    			$(self.id).veri();
				};
    		}catch(e){}
    		try{
				self.prave.oncleared=function(){
					$(self.id).veri();
				};
    		}catch(e){}
    		try{
    			self.prave.onshow = function(){
    				if('readonly'==self.readonly || 'disabled'==self.disabled){
    	    			return false;
    	    		}
    			};
    		}catch(e){}
    		self.resetWidth();
			WdatePicker(self.prave);
    	});
    	$(self.id).observe('blur',function(){//失失焦点时验证其值
    		$(self.id).veri();
    	});
    	var obj = $(self.id+'_sdDiv');
    	$(self.id).veri = function(){
    	    if(($(self.id).value.trim().length<1) && !self.allownull){//不能为空
    	    	$(self.id+'_img').show();
    	    	return false;
    	    }
    	    try{$(self.id+'_img').hide();}catch(e){}
    	    return true;
    	};
    	$(self.id).setValue = function(v){
    		$(self.id).value = v;
    		try{$(self.id+'_img').hide();}catch(e){}
    	};
    	obj.observe('mousemove',function(){
     		self.setSelectStyle(obj);
     	});
         obj.observe('mouseout',function(event){
         	var element = event.element();
         	setTimeout(function(){
                 	self.setNoSelectStyle(obj);
             }, 500);
         });
         $(self.id).setDisabled = function(a){
     		$(self.id).writeAttribute({disabled:a});
     		self.disabled=(a?'disabled':'');
     	};
     	$(self.id).setReadonly = function(a){
     		$(self.id).writeAttribute({readonly:a});
     		self.readonly=(a?'readonly':'');;
     	};
         //------------------------------------------------
         document.observe('widget:noka_reinit',function(event){//失失焦点时验证其值
       		 if(self.id.indexOf('_nkADinsert_')==-1){
    	    		var npfffs=event.memo.widgetNumber.clone();
    	    		for(var i=0;i<npfffs.length;i++){
    	    			var initem = npfffs[i];
    	    			if(initem.id.indexOf('_'+self.id+'_')!=-1){
    	    				var dacfgid = Object.allclone(self.cfg);
    	    				dacfgid.id = initem.id;
    	    				dacfgid.prave.el=dacfgid.id;
    	    				(new ndateutil(dacfgid)).show();
    	    				try{
    	    					var idx = dacfgid.id.substring(dacfgid.id.lastIndexOf('_')+1,dacfgid.id.length);
    	    					$(dacfgid.id).name=$(dacfgid.id).name.replace(/\[0\]/g,'['+idx+']');
    	    					if(undefined!=initem.value && $(dacfgid.id).name==initem.name){
    	    						$(dacfgid.id).setValue(initem.value);
    	    					}
    	    				}catch(e){}
    	    			}
    	    		}
       		 }
       	   });
         //---------------------------------------------------
    },
   //----------选种状态-----------------------
    setSelectStyle : function(obj){
    	var self = this;
    	obj.setStyle({borderColor:'#5794bf',backgroundImage:'url('+self.hImg.src+')'});
    },
    //-----------未选种状态-------------------
    setNoSelectStyle : function(obj){
    	var self = this;
    	obj.setStyle({borderColor:'#AFAFAF',backgroundImage:'url('+self.mImg.src+')'});
    },
    //-----------预加载图片-------------------
    PreLoding : function(){
    	var self = this;
    	self.hImg = new Image(); 
    	self.mImg = new Image();
    	self.hImg.src=self.cfg.rurl+'/nokatag/datepicker/skin/datePicker_hover.png';
    	self.mImg.src=self.cfg.rurl+'/nokatag/datepicker/skin/datePicker.png';
    	try{document.execCommand("BackgroundImageCache", false, true);}catch(e){};
    }
});